﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KBVault.Web.Infrastructure;
using System.Web.UI.HtmlControls;
using KBSearch;
using System.Text.RegularExpressions;
using System.Web.Services;

namespace KBVault.Web.Public
{
    public partial class Search : KBVaultPublicPage
    {
        private int CurrentPage
        {
            get
            {
                return (ViewState["Search_Page_Key"] == null) ? 1 : Convert.ToInt32(ViewState["Search_Page_Key"]);
            }
            set
            {
                ViewState["Search_Page_Key"] = value;
            }
        }

        private int ResultCount
        {
            get
            {
                return (ViewState["Search_Count_Key"] == null) ? 0 : Convert.ToInt32(ViewState["Search_Count_Key"]);
            }
            set
            {
                ViewState["Search_Count_Key"] = value;
            }
        }

        [WebMethod]
        public static string[] GetSearchTitle(string term)
        {
            Spider engine = new Spider(term,10);
            List<SpiderResult> results = engine.Search(1);
            return results.Select(s => s.Title).ToArray<String>();

        }
        private void SearchAndBind(int page)
        {
            Spider engine = new Spider(Session[Constants.SearchQueryKey].ToString(), Constants.SearchResultPageSize);
            rptSearchResults.DataSource = engine.Search(page);
            rptSearchResults.DataBind();
            btnPrev.Visible = page > 1;
            btnNext.Visible = ((page) * Constants.SearchResultPageSize) < ResultCount;
            CurrentPage = page;
            SetPageTitle("Search results for \"" + Session[Constants.SearchQueryKey].ToString() + "\"");

        }

        protected new void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (Session[Constants.SearchQueryKey] != null)
            {
                if (!Page.IsPostBack)
                {
                    Spider engine = new Spider(Session[Constants.SearchQueryKey].ToString(), Constants.SearchResultPageSize);
                    ResultCount = engine.ResultCount();
                    if (ResultCount > 0)
                    {
                        SearchAndBind(1);
                    }
                    htmlNoHits.Visible = ResultCount == 0;                   
                    htmlResults.Visible = ResultCount > 0;

                }
            }
        }

        protected void rptSearchResults_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HyperLink linkToArticle = e.Item.FindControl("linkToArticle") as HyperLink;
                HtmlGenericControl articleContent = e.Item.FindControl("articleContent") as HtmlGenericControl;
                SpiderResult res = e.Item.DataItem as SpiderResult;
                linkToArticle.Text = res.Title;
                string htmlFreeContent = Regex.Replace(Server.HtmlDecode(res.Content), "<.*?>", string.Empty);
                articleContent.InnerHtml = htmlFreeContent.Length > 200 ? htmlFreeContent.Substring(0, 200) + "..." : htmlFreeContent;
                linkToArticle.NavigateUrl = String.Format("/Public/ShowArticle.aspx?Article={0}", res.Id);

            }
        }

        protected void btnPrev_Click(object sender, EventArgs e)
        {
            if (CurrentPage > 1)
            {
                SearchAndBind(CurrentPage - 1);
            }
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (CurrentPage < ResultCount)
            {
                SearchAndBind(CurrentPage + 1);
            }
        }

    }
}